<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="goodsId" />
      <el-table-column label="商品名称" align="center" prop="erpGoods.name" width="150px" />
      <el-table-column label="供应商名称" align="center" prop="erpSuppliers.customName" width="180px" />
      <el-table-column label="平均价格" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.avgPrice ? scope.row.avgPrice.toFixed(2) : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最低价格" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.minPrice ? scope.row.minPrice.toFixed(2) : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最高价格" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.maxPrice ? scope.row.maxPrice.toFixed(2) : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商数量" align="center" prop="supplierCount" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listHistory } from "@/api/procurementmanagement/history";

export default {
  name: "History",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购价格历史表格数据
      historyList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        supplierId: null,
        dateRange: [],
        startDate: null,
        endDate: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 处理表格行选择变化 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 查询采购价格历史列表 */
    getList() {
      this.loading = true;
      listHistory(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        console.error("Error fetching data:", error);
      });
    },

    // 日期范围变化处理
    handleDateChange(val) {
      if (val && val.length === 2) {
        this.queryParams.startDate = val[0];
        this.queryParams.endDate = val[1];
      } else {
        this.queryParams.startDate = null;
        this.queryParams.endDate = null;
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
